/*
https://leetcode.cn/problems/count-the-number-of-good-nodes/description/
3249. 统计好节点的数目
*/

class Solution {
public:
vector<vector<int>>mp;
int ans=0;
int dfs(int i,int fu)
{
    int n=1;
    int k=0;
    bool flag=true;
    for(int j : mp[i])
    {
        if(j==fu) continue;
        int t=dfs(j,i);
        if(k==0)    k=t;
        else if(k!=t)    flag=false;
        n+=t;
    }
    if(flag) ans++;
    return n;
}
    int countGoodNodes(vector<vector<int>>& edges) {
        int n=edges.size()+1;
        mp.resize(n);
        for(int i=0;i<edges.size();i++)
        {
            mp[edges[i][0]].push_back(edges[i][1]);
            mp[edges[i][1]].push_back(edges[i][0]);
        }
        dfs(0,-1);
        return ans;
    }
};
